import { showToast } from "vant";
import { createRouter, createWebHashHistory } from "vue-router";

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      component: () => import('../views/home')
    },
    {
      path: '/home',
      component: () => import('../views/home')
    },
    {
      path: '/order',
      component: () => import('../views/order'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/my',
      component: () => import('../views/my'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/carts',
      component: () => import('../views/carts'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/store',
      component: () => import('../views/store')
    },
    {
      path: '/createorder',
      component: () => import('../views/createorder'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/address',
      component: () => import('../views/address'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/addressedit',
      component: () => import('../views/addressedit'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/userinfoedit',
      component: () => import('../views/userinfoedit'),
      meta: {
        isAuth: true
      }
    },
    {
      path: '/login',
      component: () => import('../views/login')
    },
    {
      path: '/register',
      component: () => import('../views/register')
    },
  ]
})
router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    // 登录之后 会在localstorge里面存储一个标识
    if (localStorage.isLogin === 'login') {
      next()
    } else {
      next('/login')
      showToast('请先登录')
    }
  } else { 
    next()
  }
})
export default router